package dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import util.DataBaseConnection;
import bean.Guia_de_salida;
import dao.Entidad;

public class MySQLGuia_de_salidaDAO implements Entidad<Guia_de_salida>{

	@Override
	public Guia_de_salida insert(Guia_de_salida t) {
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="INSERT INTO guia_de_salida(fecha,hora,descripcion)";
			sql=sql + " values(?,?,?)";
			PreparedStatement ps=con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setObject(1, t.getFecha());
			ps.setObject(2, t.getHora());
			ps.setString(3, t.getDescripcion());
			ps.executeUpdate();
			ResultSet rs=ps.getGeneratedKeys();
			if(rs.next()){
				int id=rs.getInt(1);
				t.setId_guia_de_salida(id);
			}		
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public int update(Guia_de_salida t) {
		Connection con=null;
		int rows = -1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="UPDATE guia_de_salida SET fecha=?,hora=?,descripcion=? ";
			sql = sql +	" WHERE id_guia_de_salida= ?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setObject(1, t.getFecha());
			ps.setObject(2, t.getHora());
			ps.setString(3, t.getDescripcion());
			ps.setInt(4, t.getId_guia_de_salida());
			rows =ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;	
	}

	@Override
	public int delete(Guia_de_salida t) {
		Connection con=null;
		int rows=-1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="DELETE FROM guia_de_salida WHERE id_guia_de_salida=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_guia_de_salida());
			rows=ps.executeUpdate();
			System.out.println(rows);
				
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;
	}

	@Override
	public Guia_de_salida findById(Guia_de_salida t) {
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM guia_de_salida WHERE id_guia_de_salida=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_guia_de_salida());
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				t.setId_guia_de_salida(rs.getInt("id_guia_de_salida"));
				t.setFecha(rs.getDate("fecha"));
				t.setHora(rs.getDate("hora"));
				t.setDescripcion(rs.getString("descripcion"));
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public List<Guia_de_salida> listAll() {
		List<Guia_de_salida> guia_salida = new ArrayList<Guia_de_salida>();
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="Select * from guia_de_salida";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				Guia_de_salida t = new Guia_de_salida();
				t.setId_guia_de_salida(rs.getInt("id_guia_de_salida"));
				t.setFecha(rs.getDate("fecha"));
				t.setHora(rs.getDate("hora"));
				t.setDescripcion(rs.getString("descripcion"));
				guia_salida.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return guia_salida;
	}

	@Override
	public List<Guia_de_salida> listAllCustom(Object... objects) {
		// TODO Auto-generated method stub
		return null;
	}

}
